<?php

namespace App\Http\Controllers\Index;
use App\Http\Controllers\Controller;
use App\Http\Service\CategoryService;
use Auth;
use Illuminate\Http\Request;

class IndexController extends Controller{
    /*
     * @author wangan
     * @since 2016-10-25
     * @desc 后台首页
     * */
    public function index(Request $request){
        $isAdmin = $request->session()->get('isAdmin', false);
        $menus = $this->getSlideMenu($isAdmin);

        return view('index.index',[
            'slideMenu' =>$menus,
            'isAdmin'=>$isAdmin,
        ]);
    }

    private function getSlideMenu($isAdmin=false){
        $categorys = CategoryService::getAllCategoryGroupby();
        $admin=config('admincategory');
        $categorys[]=$admin;
        if($isAdmin||Auth::user()->id==1){
            return $categorys;
        }
        foreach ($categorys as $key => $val){
            foreach ($val['childs'] as $kk => $vv){
                if(!Auth::user()->can($vv['name'])){
                    unset($categorys[$key]['childs'][$kk]);
                }
            }
        }

        return $categorys;
    }

}